<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Evan
 * Date: 12-11-8
 * Time: 下午11:06
 * To change this template use File | Settings | File Templates.
 */
class Model_Cfg_SearchGroup extends Jwawa_BaseModel
{
    protected static $validators = array(
        'id' => array(
            'rules' => array('Int' => null),
            'require' => true,
            'for' => array('update')
        ),
        'page_id' => array(
            'rules' => array('Int' => null),
            'require' => true,
            'for' => array('create', 'update')
        ),
        'group_name' => array(
            'rules' => array(
                'StringLength' => array(
                    'min' => 3,
                    'max' => 50,
                    'messages' => array(Zend_Validate_StringLength::INVALID =>
                                        '查询名称的长度必须在 %min% and %max% 之间')
                )
            ),
            'require' => true
        ),
        'seq' => array(
            'rules' => array('Int' => null),
            'require' => true
        ),
        'is_default' =>  array(
            'rules' => array(
                'InArray' => array('Y', 'N')
            ),
            'require' => true
        ),
        'create_by' => array(
            'rules' => array(
                'StringLength' => array('min' => 1,'max' => 10)
            ),
            'require' => true,
            'for' => array('create')
        ),
        'update_by' => array(
            'rules' => array(
                'StringLength' => array('min' => 1,'max' => 10)
            ),
            'require' => true
        ),
        'create_time' => array(
            'rules' => array(
                'Date' => array(
                    'format' => 'yyyy-MM-dd HH:mm:ss'
                )
            ),
            'require' => true,
            'for' => array('create')
        ),
        'update_time' => array(
            'rules' => array(
                'Date' => array(
                    'format' => 'yyyy-MM-dd HH:mm:ss'
                )
            ),
            'require' => true
        ),
    );

    public static function saveOrUpdate($searchGroup){
        if(empty($searchGroup->id)){
            $searchGroup = self::save($searchGroup);
        } else {
            $searchGroup = self::update($searchGroup);
        }

        return $searchGroup;
    }

    public function update($searchGroup){
        Jwawa_Validate_Factory::validate(self::$validators, $searchGroup, 'update');

        $sql = "UPDATE cfg_search_groups
                SET page_id = :page_id,
                    group_name = :group_name,
                    is_default = :is_default,
                    seq = :seq,
                    update_time = :update_time,
                    update_by = :update_by
                WHERE id = :id";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->setProperties($searchGroup);
        $stmt->execute();

        return $searchGroup;
    }

    public static function save($searchGroup){
        Jwawa_Validate_Factory::validate(self::$validators, $searchGroup);

        $sql = 'INSERT INTO cfg_search_groups(page_id, group_name, seq, create_time, update_time, create_by, update_by, is_default)
                VALUES (:page_id, :group_name, :seq, :create_time, :update_time, :create_by, :update_by,  :is_default)';
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->setProperties($searchGroup);
//        $stmt->bindValue('action_id', $searchGroup->action_id);
//        $stmt->bindValue('group_name', $searchGroup->group_name);
//        $stmt->bindValue('seq', $searchGroup->seq);
//        $stmt->bindValue('create_time', $searchGroup->create_time);
//        $stmt->bindValue('update_time', $searchGroup->update_time);
//        $stmt->bindValue('create_by', $searchGroup->create_by);
//        $stmt->bindValue('update_by', $searchGroup->update_by);
//        $stmt->bindValue('is_default', $searchGroup->is_default);

        if($rst = $stmt->execute()){
           $id = self::getDb()->lastInsertId();
           $searchGroup->id = intval($id);
           return $searchGroup;
        } else {
            return null;
        }
    }

    public static function findSearchGrpByPageId($pageId){
        $sql = "SELECT csg.id,
                  csg.group_name,
                  csg.page_id,
                  csg.seq,
                  csg.is_default,
                  csg.create_time
                FROM cfg_search_groups csg
                WHERE csg.page_id = :page_id";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('page_id', $pageId);
        $stmt->execute();
        $rst = $stmt->fetchAllObj();

        return $rst;
    }

    public static function findSearchGrpByPageCode($pageCode){
        $sql = "SELECT csg.id,
                  csg.group_name,
                  csg.page_id,
                  csg.seq,
                  csg.is_default,
                  csg.create_time
                FROM cfg_search_groups csg
                join cfg_search_pages csp on csp.id = csg.page_id
                WHERE csp.page_code = :page_code";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('page_code', $pageCode);
        $stmt->execute();
//        $rowList = $stmt->fetchAll(PDO::FETCH_OBJ);
//        $rst = self::getInstanstList($rowList);

//        $rst = $stmt->fetchAllObj(get_called_class());
        $rst = $stmt->fetchAllObj();

        return $rst;
    }

    public static function isExistSearchGroupByName($pageId, $groupName){
        $sql = "SELECT count(*) c
                FROM cfg_search_groups csg
                WHERE csg.group_name = :group_name
                  AND csg.page_id = :page_id";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('group_name', $groupName);
        $stmt->bindValue('page_id', $pageId);
        $stmt->execute();
        $rst = $stmt->fetchObj();;

        return $rst->c == 0 ? false: true;
    }

    public static function getMaxSeqByPageId($pageId){
        if(!$pageId){
            throw new Jwawa_BusinessException(500, 'pageId cannot be null');
        }

        $sql = 'SELECT max(seq) max_seq
            FROM cfg_search_groups csg
            WHERE csg.page_id = :page_id';

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('page_id', $pageId);
        $stmt->execute();
        if($rst = $stmt->fetchObj()){
            return intval($rst->max_seq);
        }else{
            return 0;
        }
    }

    public static function deleteByIds($searchGroupIdList){
        if(!$searchGroupIdList){
            throw new Jwawa_BusinessException(500, 'searchGroupIds cannot be null');
        }
        for($i = 0; $i < count($searchGroupIdList); $i++){
            $validator = new Jwawa_Validate_Int();
            if(!$validator->isValid($searchGroupIdList[$i])){
                $message = implode(',\n', $validator->getMessages());
                throw new Jwawa_BusinessException('VALIDATE_ERROR', null,$message);
            }
        }

        $searchGroupIds = implode(',', $searchGroupIdList);
        $sql = "DELETE FROM cfg_search_groups WHERE id in ($searchGroupIds)";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->execute();
    }

    public static function updateSearchGroupToNoneDefaultByPageId($pageId){
        if(!$pageId){
            throw new Jwawa_BusinessException(500, 'actionId cannot be null');
        }

        $sql = "UPDATE cfg_search_groups SET is_default = 'N' where page_id = :page_id";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('page_id', $pageId);
        $stmt->execute();
    }

    public static function setSearchGroupToDefault($searchGroup){
        if(!$searchGroup){
            throw new Jwawa_BusinessException(500, 'searchGroupId cannot be null');
        }

        $sql = "UPDATE cfg_search_groups
                SET is_default = 'Y',
                    update_by = :update_by,
                    update_time = :update_time
                WHERE id = :id";
        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->setProperties($searchGroup);
        $stmt->execute();
    }

    public static  function getSearchGroupById($searchGroupId){
        $sql = "SELECT csg.id,
                  csg.group_name,
                  csg.page_id,
                  csg.seq,
                  csg.is_default,
                  csg.create_time
                FROM cfg_search_groups csg
                WHERE csg.id = :id";

        $stmt = self::createDefaultStmt(self::getDb(), $sql);
        $stmt->bindValue('id', $searchGroupId);
        $stmt->execute();
        $rst = $stmt->fetchObj(get_called_class());;

        return $rst;
    }
}